package com.lib.torment.androidlib.net;

import android.content.Context;

import com.lib.torment.androidlib.callback.ResponseCallBack;
import com.lib.torment.androidlib.callback.RetrofitClientCallBack;
import com.lib.torment.androidlib.entity.AppEntity;
import com.lib.torment.androidlib.interfaces.IDialogProgress;
import com.lib.torment.androidlib.net.dialog.SystemDefaultProgressDialog;
import com.lib.torment.androidlib.widget.toast.ToastFactory;

import rx.Observable;
import rx.Subscriber;

/**
 * Created by Torment on 2016/9/30.
 */
public abstract class BaseRetrofitRequest<T extends AppEntity<R>, R> implements ResponseCallBack<R> {
    public Context mContext;

    public Observable<T> mObservable;
    public ResponseCallBack<R> mIResponseCallBack;
    public RetrofitClientCallBack<T> mRetrofitClientCallBack;

    // 是否显示加载中的 Dialog
    public boolean mIsLoadingDialog;
    // 实现IDialogProgress，实现自己的Dialog样式
    public IDialogProgress mDialogProgressListener;

    // 当调用onFailure，是否显示错误的Toast
    public boolean isErrorToast = true;

    public BaseRetrofitRequest(Context context) {
        this.mContext = context;
    }

    /**
     * 异步请求方法
     *
     * @param retrofitClientCallBack 客户端回调
     * @param iResponseCallBack      服务器的Service
     */
    public void asyncExecute(RetrofitClientCallBack<T> retrofitClientCallBack,
                             ResponseCallBack<R> iResponseCallBack) {
        mIResponseCallBack = iResponseCallBack;
        mRetrofitClientCallBack = retrofitClientCallBack;

        if (mRetrofitClientCallBack == null || mIResponseCallBack == null) {
            throw new RuntimeException("IRetrofitClientCallBack or IResponseCallBack is null");
        }

        loadingDialogCheck();

        mObservable = mRetrofitClientCallBack.getObservable();
        Subscriber<T> subscriber = setSubscriber(this);
        mObservable.subscribe(subscriber);
    }

    protected abstract Subscriber<T> setSubscriber(ResponseCallBack<R> iResponseCallBack);

    protected void loadingDialogCheck() {
        if (mIsLoadingDialog && mDialogProgressListener == null) {
            mDialogProgressListener = new SystemDefaultProgressDialog(mContext, "请稍后...");
            mDialogProgressListener.showDialog();
        } else if (!mIsLoadingDialog && mDialogProgressListener != null) {
            mDialogProgressListener.showDialog();
        }
    }

    @Override
    public void onSuccess(R result) {
        if (mDialogProgressListener != null) {
            mDialogProgressListener.hideDialog();
        }

        boolean success = onSuccessCallBack(result);
        if (success) {
            mIResponseCallBack.onSuccess(result);
        }
    }

    @Override
    public void onFailure(String code, String message, boolean isHttpError) {
        if (mDialogProgressListener != null) {
            mDialogProgressListener.hideDialog();
        }

        boolean failure = onFailureCallBack(code, message, isHttpError);

        if (failure) {
            if (isErrorToast) {
                setErrorToast(mContext, code, message, isHttpError);
            }
            mIResponseCallBack.onFailure(code, message, isHttpError);
        }
    }

    public void setErrorToast(Context context, String code, String message, boolean isHttpError) {
        ToastFactory.showStandard(context, message, isHttpError ? setErrorHttpIcon() : setErrorIcon());
    }

    public abstract boolean onSuccessCallBack(R result);

    /**
     * @param code
     * @param message
     * @param isHttpError
     * @return 是否执行下面的操作
     */
    public abstract boolean onFailureCallBack(String code, String message, boolean isHttpError);

    public abstract int setErrorHttpIcon();

    public abstract int setErrorIcon();
}